﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MJM.SEDIC.EN;
using MJM.CBD.Conn;

namespace MJM.SEDIC.AD
{
    public static class MenuAD
    {
        public static int Insertar(MenuEN obj)
        {
            int codigo = 1;
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaVecinos());
            SqlCommand cmd = new SqlCommand("sp_i_T_MAES_MENU_Insertar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vMENU_chNOMMEN", obj.Nombre));
                cmd.Parameters.Add(new SqlParameter("@vMENU_chDESMEN", obj.Descripcion));
                cmd.Parameters.Add(new SqlParameter("@vMENU_inORDEN", obj.Orden));

                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();

                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                codigo = 0;
                cmd.Dispose();
                conn.Dispose();
            }
            return codigo;
        }

        public static List<MenuEN> Listar()
        {
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaVecinos());
            SqlCommand cmd = new SqlCommand("sp_s_T_MAES_MENU_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            List<MenuEN> lst = new List<MenuEN>();

            try
            {
                cmd.Connection.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    var obj = new MenuEN();
                    obj.ID = dr.GetInt32(dr.GetOrdinal("MENU_P_inID"));
                    obj.Descripcion = dr.GetString(dr.GetOrdinal("MENU_chDESMEN"));
                    obj.Nombre = dr.GetString(dr.GetOrdinal("MENU_chNOMMEN"));
                    obj.Orden = dr.GetInt32(dr.GetOrdinal("MENU_inORDEN"));
                    lst.Add(obj);
                }
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }
            return lst;
        }

        public static int Actualizar(MenuEN obj)
        {
            int result = 1;
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaVecinos());
            SqlCommand cmd = new SqlCommand("sp_u_T_MAES_MENU_Actualizar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vMENU_P_inID", obj.ID));
                cmd.Parameters.Add(new SqlParameter("@vMENU_chNOMMEN", obj.Nombre));
                cmd.Parameters.Add(new SqlParameter("@vMENU_chDESMEN", obj.Descripcion));
                cmd.Parameters.Add(new SqlParameter("@vMENU_inORDEN", obj.Orden));
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                result = 0;
                cmd.Dispose();
                conn.Dispose();
            }
            return result;
        }
    }
}
